package com.xxxx.server.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.http.server.HttpServerResponse;
import com.xxxx.server.pojo.Lectureinfo;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.RespPageBean;
import com.xxxx.server.service.ILectureinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LT
 * @since 2022-02-27
 */
@Api(value="讲座信息管理",tags = "讲座信息管理")
@RestController
@RequestMapping("")
public class LectureinfoController {
    @Autowired
    private ILectureinfoService lectureinfoService;


    @ApiOperation(value = "获取所有讲座信息")
    @GetMapping("/system/LectureInfoManagement/list")
    public List<Lectureinfo>getAllLectureInfo(){
        return lectureinfoService.list();
    }

    @ApiOperation(value = "获取所有讲座信息并分页显示")
    @GetMapping("/system/LectureInfoManagement/getLectureInfoByPage/list")

    public RespPageBean getAllLectureInfoByPage(
            @RequestParam(defaultValue = "1") Integer currentPage
            ,@RequestParam(defaultValue = "5") Integer size
            ,Lectureinfo lectureinfo)
    {
        return lectureinfoService.getAllLectureInfoByPage(currentPage,size,lectureinfo);
    }

    @ApiOperation(value = "通过讲座关键字查询讲座信息")
    @GetMapping("/system/LectureInfoManagement/keywordsList")
    public List<Lectureinfo>getAllLectureInfoByKeyWords(String keywords){
        return lectureinfoService.getAllLectureInfoByKeyWords(keywords);

    }

    @ApiOperation(value = "通过讲座ID查询讲座信息")
    @GetMapping("/system/LectureInfoManagement/lectureInfoByLectureId")
    public List<Lectureinfo>getAllLectureInfoByKeyWords(Integer LectureId){
        return lectureinfoService.getAllLectureInfoByLectureId(LectureId);

    }


    @ApiOperation(value = "添加讲座信息")
    @PostMapping("/system/LectureInfoManagement/addLectureInfo/add")
    public RespBean addLectureInfo(@RequestBody Lectureinfo lectureinfo){
//
//        lectureinfo.setBeginDate(LocalDateTime.now());//设置当前创建时间
//       lectureinfo.setEndDate(LocalDateTime.now());//设置当前创建时间

        if (lectureinfoService.save(lectureinfo)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "更新讲座信息")
    @PutMapping("/system/LectureInfoManagement/update")
    public RespBean updatePosition(@RequestBody Lectureinfo lectureinfo){
        if (lectureinfoService.updateById(lectureinfo)){
            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除讲座信息")
    @DeleteMapping("/system/LectureInfoManagement/delete")
    public RespBean deletePosition(@RequestParam("id") Integer id){
        if (lectureinfoService.removeById(id)){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "批量删除讲座信息")
    @DeleteMapping("/system/LectureInfoManagement/deletes")
    public RespBean deletePositionByIds(@RequestParam Integer[] ids){
        System.out.println("删除岗位信息");
        if (lectureinfoService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "导出讲座信息数据")
    @GetMapping(value = "/system/LectureInfoManagement/downloads/",produces = "application/octet-stream")
    public void downloadsLectureInfo(HttpServletResponse response)
    {
        //查询所有
        List<Lectureinfo> list = lectureinfoService.downloadsLectureInfo(null);
        //文件名,表名,导出格式
        ExportParams params=new ExportParams("讲座信息表","讲座信息表"
                , ExcelType.HSSF);//2007 excel 格式

        Workbook workbook = ExcelExportUtil.exportExcel(params, Lectureinfo.class, list);
        ServletOutputStream outputStream= null;
     try
     {
         //流形式
         response.setHeader("content-type","application/octet-stream");
         //防止中文乱码
         response.setHeader("content-disposition","attachment;filename="
                 + URLEncoder.encode("讲座信息表.xls","UTF-8"));
         outputStream= response.getOutputStream();
         workbook.write(outputStream);


     }
     catch (IOException e)
     {
         e.printStackTrace();
     }
     finally {
         if (outputStream!=null)
         {
             try {
                 outputStream.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

     }

    }
    @ApiOperation(value = "导入讲座信息")
    @PostMapping("/system/LectureInfoManagement/import/")
    public RespBean importLectureInfo(MultipartFile file)
    {
        ImportParams importParams = new ImportParams();
        //表格内第一行不导入
        importParams.setTitleRows(1);
        try {
            List<Lectureinfo> lectureinfoList=ExcelImportUtil.
                    importExcel(file.getInputStream(),Lectureinfo.class,importParams);

            System.out.println(lectureinfoList);
            if(lectureinfoService.saveBatch(lectureinfoList))
            {
                return RespBean.success("导入成功！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error("导入失败");
    }
}
